<template>
  <el-dialog :close-on-click-modal="closeModal" :visible.sync="form_detail_drawer.formVisible" :title="title" width="45%" :before-close="handleClose">
    <el-form ref="ruleForm" :rules="rules" class="demo-ruleForm" :inline="true" :model="form" label-width="180px">
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="form.realName" style="width: 15vw" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" style="width: 15vw" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" style="width: 15vw" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" style="width: 15vw" placeholder="请输入密码" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit('ruleForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { submitSysUser } from '@/api/user.js'
import { validatePassword, validatePhone } from '@/utils/formCheck'
export default {
  name: 'Dialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    closeModal: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    getList: {
      type: Function,
      default: null
    },
    disabled: {
      type: Boolean,
      default: true
    },
    dataSource: Object
  },
  data() {
    return {
      form_detail_drawer: {
        formVisible: false
      },
      postForm: {
        currentPage: 1,
        pageSize: 10
      },

      rules: {
        realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入长度为6至12的用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    form() {
      return this.dataSource
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.form_detail_drawer.formVisible = newVal
    }
  },
  created() {},
  methods: {
    // 确定
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验通过
          submitSysUser(this.$filNull(this.form)).then(res => {
            const { result, msg, data } = res
            if (result == 1) {
              this.$message.success(msg)
              this.getList(this.postForm)
              this.handleClose()
            } else {
              this.$message.warning(msg)
              // this.handleClose()
            }
          })
        } else {
          console.log('校验未通过')
          return false
        }
      })
    },
    // 取消   关闭弹窗
    handleClose() {
      this.$emit('close')
      this.form = {}
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-drawer__footer {
  margin-top: 50px;
  display: flex;
  justify-content: center;
}
</style>

<style>
.el-drawer {
  overflow: scroll;
}
</style>
